
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.microedition.io.StreamConnection;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class pos extends javax.swing.JFrame implements ActionListener{

    private StreamConnection con = null;
    private Thread runUp = null;
    private DataOutputStream out = null;
    private DataInputStream in = null;
    public String  newLine = "",custId = null,time= null, CustTable= null, order = null,Price = null, datee= null,recieptNo= null;
private  StringBuffer OrderBff= null;
private int amount=0, bald=0,i=0,k=0,h=0;
    public pos(StreamConnection c) {
        this.con = c;
        initComponents();
        savbtn.addActionListener(this);
        cashName.setVisible(false);
        cashier.setVisible(false);
        lock.setEnabled(false);
        unqueId.setEnabled(false);
        search.setEnabled(false);
        msg.setVisible(false);
        ampaid.setEnabled(false);
        savbtn.setEnabled(false);
        msg2.setVisible(false);
        table.setVisible(false);
        table.setEnabled(false);
        loginlabel.setVisible(false);
        image.setVisible(false);
        
     //   msg.setVisible(false);
        runUp = new Thread() {

            public void run() {
                table.getTableHeader().setResizingAllowed(true);
                table.getTableHeader().setReorderingAllowed(false);
                
                while (true) {
                    try {
                        out = con.openDataOutputStream();
                        in = new DataInputStream(con.openInputStream());
                    } catch (IOException ex) {
                    }

                    try {

                        for (;;) {
                        recieve(in.readUTF());
                        }

                    } catch (IOException ex) {
                    }
                }
            }
        };
        runUp.start();
    }

    synchronized void recieve(String str) {
        if(str.equals("No")){
        loginlabel.setVisible(true);
        loginlabel.setText("* Wrong Username or Password");
        username.setText("");
        passwd.setText("");
        }else{
        if(str.startsWith("+")){
        loginlabel.setVisible(false);
        login.setEnabled(false);
        username.setEnabled(false);
        passwd.setEnabled(false);
        lock.setEnabled(true);
        unqueId.setEnabled(true);
        search.setEnabled(true);
        username.setText("");
        passwd.setText("");
        cashName.setVisible(true);
        cashier.setText(str.substring(str.indexOf("+")+1 ,str.length()));
        cashier.setVisible(true);
        image.setVisible(false);
        if(table.getRowCount()>0){
            table.setVisible(true);
            ampaid.setEnabled(true);
            search.setEnabled(false);
            unqueId.setEnabled(true);
        }
       }else if(str.equals("DD")){
         msg.setVisible(true);
         msg.setText("* Already Paid Record*");
         msg2.setText("");
          if(table.getRowCount()>0){
         ((DefaultTableModel) table.getModel()).removeRow(0);
             }
        }else if(str.equals("NoRcd")) {
         msg.setVisible(true);
         msg.setText("* No records *");
         msg2.setText("");
          if(table.getRowCount()>0){
         ((DefaultTableModel) table.getModel()).removeRow(0);
             }
        }else{

            ampaid.setEnabled(true);
            savbtn.setEnabled(false);
            table.setVisible(true);
            msg2.setVisible(true);
            msg.setVisible(false);
            
            custId=str.substring(0, str.indexOf("!")-1);
            CustTable=str.substring(str.indexOf("!")+1,str.indexOf("@")-1);
            order =str.substring(str.indexOf("@")+1,str.indexOf("#")-1);
            Price =str.substring(str.indexOf("#")+1,str.indexOf("$")-1);
            datee=str.substring(str.indexOf("$")+1,str.indexOf("%")-1);
            time=str.substring(str.indexOf("%")+1,str.indexOf("^")-1);
            recieptNo=str.substring(str.indexOf("^")+1,str.length());
            
            msg2.setText("Records Available for : "+custId);
            table.getColumnModel().getColumn(2).setCellRenderer(new Tarea());
             if(table.getRowCount()>0){
         ((DefaultTableModel) table.getModel()).removeRow(0);
             }
            Object data[] = {custId,CustTable,order.toString() ,Price,recieptNo,time};
            ((DefaultTableModel) table.getModel()).insertRow(0, data);

            //release resources
              data = new Object[0];
              custId=null;
              time=null;
              CustTable=null;
              order=null;
              Price=null;
              recieptNo=null;
              datee=null;
        }
       // }
        
        }     
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel4 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        jPanel6 = new javax.swing.JPanel();
        loginlabel = new javax.swing.JLabel();
        username = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        passwd = new javax.swing.JPasswordField();
        login = new javax.swing.JButton();
        jPanel9 = new javax.swing.JPanel();
        cashName = new javax.swing.JLabel();
        cashier = new javax.swing.JLabel();
        lock = new javax.swing.JButton();
        logout = new javax.swing.JButton();
        image = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jPanel8 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        ampaid = new javax.swing.JTextField();
        bal = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel26 = new javax.swing.JLabel();
        unqueId = new javax.swing.JTextField();
        search = new javax.swing.JButton();
        msg = new javax.swing.JLabel();
        savbtn = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        msg2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Point of Sale App");

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));

        jPanel2.setBackground(new java.awt.Color(0, 0, 0));

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/banner.png"))); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 1030, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel6)
                .addContainerGap())
        );

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bot.png"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 1030, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        table.setBackground(new java.awt.Color(204, 204, 255));
        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "CustID", "CustTable", "Order", "Price", "RecieptNo", "Time"
            }
        ));
        table.setFillsViewportHeight(true);
        jScrollPane1.setViewportView(table);

        jPanel6.setBackground(new java.awt.Color(182, 190, 190));
        jPanel6.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        loginlabel.setFont(new java.awt.Font("Arial", 0, 11));
        loginlabel.setForeground(new java.awt.Color(255, 0, 0));
        loginlabel.setText("Wrong Username or Password");

        username.setFont(new java.awt.Font("Arial", 0, 11));

        jLabel8.setFont(new java.awt.Font("Arial", 0, 11));
        jLabel8.setText("Username");

        jLabel9.setFont(new java.awt.Font("Arial", 0, 11));
        jLabel9.setText("Password");

        passwd.setFont(new java.awt.Font("Arial", 0, 11));

        login.setText("Login");
        login.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginActionPerformed(evt);
            }
        });

        jPanel9.setBackground(new java.awt.Color(182, 190, 190));

        cashName.setFont(new java.awt.Font("Arial", 0, 11));
        cashName.setText("Cashier Name:");

        cashier.setFont(new java.awt.Font("Arial", 0, 11));
        cashier.setForeground(new java.awt.Color(0, 102, 102));
        cashier.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        cashier.setText("Cathy");

        lock.setFont(new java.awt.Font("Arial", 0, 10));
        lock.setText("Lock");
        lock.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lockActionPerformed(evt);
            }
        });

        logout.setFont(new java.awt.Font("Arial", 0, 10));
        logout.setText("Close");
        logout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logoutActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addContainerGap(45, Short.MAX_VALUE)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel9Layout.createSequentialGroup()
                        .addComponent(cashName, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cashier, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel9Layout.createSequentialGroup()
                        .addComponent(lock, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(logout, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cashName)
                    .addComponent(cashier))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(logout)
                    .addComponent(lock)))
        );

        image.setIcon(new javax.swing.ImageIcon(getClass().getResource("/padlock.png"))); // NOI18N
        image.setToolTipText("Application is Locked. Login again");

        jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel9)
                        .addGap(18, 18, 18)
                        .addComponent(passwd, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(login)
                        .addGap(68, 68, 68))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(loginlabel, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 288, Short.MAX_VALUE)
                        .addComponent(image)
                        .addGap(77, 77, 77)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(100, 100, 100)
                .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(loginlabel)
                    .addComponent(image))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(passwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(login))
                .addContainerGap(20, Short.MAX_VALUE))
            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel8.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel2.setFont(new java.awt.Font("Arial", 0, 12));
        jLabel2.setText("Amount Paid:");

        jLabel3.setFont(new java.awt.Font("Arial", 0, 12));
        jLabel3.setText("Balance:");

        ampaid.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        ampaid.setToolTipText("Amount the Customer Pays.");
        ampaid.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                ampaidKeyTyped(evt);
            }
            public void keyPressed(java.awt.event.KeyEvent evt) {
                ampaidKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                ampaidKeyReleased(evt);
            }
        });

        bal.setFont(new java.awt.Font("Tahoma", 0, 14));
        bal.setText("0");
        bal.setToolTipText("Amount to be returned to Customer");
        bal.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        bal.setEnabled(false);

        jLabel1.setBackground(new java.awt.Color(0, 0, 0));
        jLabel1.setFont(new java.awt.Font("Arial", 0, 12));
        jLabel1.setText("Search using Customer's ID");

        jLabel26.setIcon(new javax.swing.ImageIcon(getClass().getResource("/search2.png"))); // NOI18N

        unqueId.setFont(new java.awt.Font("Tahoma", 0, 14));
        unqueId.setToolTipText("Enter the customer's ID as indicated on reciept.");
        unqueId.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        search.setText("Search");
        search.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchActionPerformed(evt);
            }
        });

        msg.setFont(new java.awt.Font("Arial", 0, 10));
        msg.setForeground(new java.awt.Color(255, 0, 8));
        msg.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        msg.setText("No Records Found.");

        savbtn.setText("Save Order");
        savbtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                savbtnActionPerformed(evt);
            }
        });

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel8Layout.createSequentialGroup()
                        .addComponent(jLabel26)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel8Layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(search)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(msg, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(unqueId, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(38, 38, 38)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(bal)
                    .addComponent(ampaid, javax.swing.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE))
                .addGap(58, 58, 58)
                .addComponent(savbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(97, 97, 97))
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel8Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel8Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(unqueId, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
                                    .addComponent(jLabel26))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(search)
                                    .addComponent(msg)))
                            .addGroup(jPanel8Layout.createSequentialGroup()
                                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(ampaid, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel8Layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                                        .addComponent(jLabel3)
                                        .addGap(19, 19, 19))
                                    .addGroup(jPanel8Layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(bal, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                    .addGroup(jPanel8Layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(savbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        msg2.setFont(new java.awt.Font("Arial", 0, 14));
        msg2.setForeground(new java.awt.Color(255, 0, 0));
        msg2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        msg2.setText(".....");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, 1006, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(msg2, javax.swing.GroupLayout.DEFAULT_SIZE, 1006, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 1026, Short.MAX_VALUE)
            .addComponent(jPanel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(msg2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void searchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchActionPerformed
        try {
            if(unqueId.getText().equals("")){
                msg.setVisible(true);
                msg.setText("Provide the Customer's Id.");
            }else{
                msg.setVisible(true);
                out.writeUTF("* " +unqueId.getText().trim());
                out.flush();
                    msg.setEnabled(true);
                    msg.setText("Retrieving Records for: "+unqueId.getText());
                    unqueId.setText("");
                           }
            //code for selecting from database
        } catch (IOException ex) {}
}//GEN-LAST:event_searchActionPerformed

    private void savbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_savbtnActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_savbtnActionPerformed

    private void ampaidKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_ampaidKeyTyped
      
       char c = evt.getKeyChar();
        if(!(Character.isDigit(c))){
        evt.consume();
        }
      
    }//GEN-LAST:event_ampaidKeyTyped

    private void loginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginActionPerformed
   char[] thePasswd = passwd.getPassword();
   String passwdString = new String(thePasswd);
        if(username.getText().equals("")|| passwdString.equals("")){
           loginlabel.setVisible(true);
           loginlabel.setText("* Fill in all fields");
        }else{
        try {
                out.writeUTF("! " + passwdString + " @ " + username.getText().trim());
                out.flush();
        } catch (IOException ex) { }
       }
    }//GEN-LAST:event_loginActionPerformed

    private void lockActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lockActionPerformed
        image.setVisible(true);
        cashName.setVisible(false);
        cashier.setVisible(false);
        lock.setEnabled(true);
        unqueId.setEnabled(false);
        search.setEnabled(false);
        msg.setVisible(false);
        ampaid.setEnabled(false);
        savbtn.setEnabled(false);
        msg2.setVisible(false);
        table.setVisible(false);
        table.setEnabled(false);
        loginlabel.setVisible(true);
        loginlabel.setText("Screen Locked. Please login.");
        username.setEnabled(true);
        passwd.setEnabled(true);
        login.setEnabled(true);
        lock.setEnabled(false);
        ampaid.setText("");
        bal.setText("0");
    }//GEN-LAST:event_lockActionPerformed

    private void logoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logoutActionPerformed
         int response = JOptionPane.showConfirmDialog(this, "Are you sure you want to close the application?", "POS Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);

            if (response == JOptionPane.YES_OPTION) {
            try {
                if(con!=null){
                out.writeUTF("exit");
                out.flush();
                out.close();
                in.close();
                con.close();
                System.exit(0);
                }
                 System.exit(0);
                
                
            } catch (IOException ex) {}
                    }
    }//GEN-LAST:event_logoutActionPerformed

    private void ampaidKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_ampaidKeyPressed
       if(evt.getKeyCode()==KeyEvent.VK_ENTER){
           if(ampaid.getText().equals("")){
           bal.setText("0");
           savbtn.setEnabled(false);
           }else{
       amount= Integer.parseInt(table.getValueAt(0, 3).toString().trim() );
        bald=Integer.parseInt(ampaid.getText().toString().trim());
         if(bald>=amount){
         bal.setText(""+(bald-amount));
         savbtn.setEnabled(true);
         msg2.setText("");
         }else{
         bal.setText("0");
         savbtn.setEnabled(false);
         msg2.setEnabled(true);
         msg2.setText("Amount paid still less than total." );
          }
           }
     
           
        }
    }//GEN-LAST:event_ampaidKeyPressed

    private void ampaidKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_ampaidKeyReleased
if((evt.getKeyCode()==KeyEvent.VK_BACK_SPACE)||(evt.getKeyCode()==KeyEvent.VK_DELETE) ){
savbtn.setEnabled(false);
bal.setText("0");
}
    }//GEN-LAST:event_ampaidKeyReleased
    /**
     * @param args the command line arguments
     */
    /*public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new pos().setVisible(true);
    }
    });
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField ampaid;
    private javax.swing.JTextField bal;
    private javax.swing.JLabel cashName;
    private javax.swing.JLabel cashier;
    private javax.swing.JLabel image;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JButton lock;
    private javax.swing.JButton login;
    private javax.swing.JLabel loginlabel;
    private javax.swing.JButton logout;
    private javax.swing.JLabel msg;
    private javax.swing.JLabel msg2;
    private javax.swing.JPasswordField passwd;
    private javax.swing.JButton savbtn;
    private javax.swing.JButton search;
    private javax.swing.JTable table;
    private javax.swing.JTextField unqueId;
    private javax.swing.JTextField username;
    // End of variables declaration//GEN-END:variables

    public void actionPerformed(ActionEvent e) {
     if(e.getSource()==savbtn){
        if (table.getRowCount() > 0) {
          if(ampaid.getText().equals("" )){
            JOptionPane.showMessageDialog(this, "Enter the amount paid", "POS Confirmation", JOptionPane.ERROR_MESSAGE);
                    
        }else{
       /*  if(Integer.parseInt(ampaid.toString().trim())<(Integer.parseInt(table.getValueAt(0, 3).toString().trim() ))){
 JOptionPane.showMessageDialog(this, "Pls check the Amount the customer paid.", "POS Confirmation", JOptionPane.ERROR_MESSAGE);
         }*///else{
 int index[];
                    JDialog.setDefaultLookAndFeelDecorated(true);

                    int response = JOptionPane.showConfirmDialog(this, "Do you want to Save the Order\n for: " + table.getValueAt(0, 0) + " ?", "POS Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);

                    if (response == JOptionPane.YES_OPTION) {
                            try {

                                /***************Send order to the server for storage**********************/
                               
                                out.writeUTF(table.getValueAt(0, 0)+" ! "+table.getValueAt(0, 1)
                                +" ? "+table.getValueAt(0, 2)+" $ "+table.getValueAt(0, 3)+
                                " * "+table.getValueAt(0, 4)+" % "+table.getValueAt(0, 5)+" ^ "+bald+" # "
                                +cashier.getText().trim()+" ( "+ampaid.getText().trim()+" / "+datee);
                                out.flush();

                                  msg.setVisible(false);
                                  msg2.setText("Order Saved." );
                                  ampaid.setText("");
                                  ampaid.setEnabled(false);
                                  table.setVisible(false);
                                  savbtn.setEnabled(false);
                                  unqueId.setEnabled(true);
                                  search.setEnabled(true);
                                  unqueId.setText("");
                                  bal.setText("");
                                  bald=0;
                                  amount=0;

                             ((DefaultTableModel) table.getModel()).removeRow(0);
                 } catch (IOException ex) {

                            }
                    }

       //  }
        }
        } else {
            msg.setVisible(false);
                JOptionPane.showMessageDialog(this, "No Records Found. Submit UnqueNo.", "POS Confirmation", JOptionPane.ERROR_MESSAGE);
          }
     }
    }
}
class Tarea extends JTextArea implements TableCellRenderer {

    public Tarea() {
        setLineWrap(true);
        setWrapStyleWord(true);
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        setText((String) value);
        //setAlignmentX(CENTER_ALIGNMENT);
setSize(table.getColumnModel().getColumn(column).getWidth(),getPreferredSize().height );
        if(table.getRowHeight(row)!=getPreferredSize().height){
        table.setRowHeight(row, getPreferredSize().height);
        }

        if (isSelected) {
            setForeground(table.getSelectionForeground());
            setBackground(table.getSelectionBackground());
        } else {
            setForeground(table.getForeground());
            setBackground(table.getBackground());
        }
       // setFont(table.getFont());
        if (hasFocus) {
           if (table.isCellEditable(row, column)) {
           setForeground(UIManager.getColor("Table.focusCellForeground"));
           setBackground(UIManager.getColor("Table.focusCellBackground"));
            }
        }


        return this;
    }
}
